#include "stm32f10x.h"
#include "bsp_led.h"
#include "systick.h"
#include "bsp_buzz.h"   
#include "bsp_key.h"

void LED_TASK(void)
{
	uint32_t i=0;
		for(i=0;i<3;i++)
		{
			void Systick_LED_1(void);
		}
		SysTick_Delay_Nms(300);
		for(i=0;i<3;i++)
		{
			void Systick_LED_2(void);
		}
		SysTick_Delay_Nms(300);
		for(i=0;i<3;i++)
		{
			void Systick_LED_3(void);
		}
		SysTick_Delay_Nms(300);
		for(i=0;i<3;i++)
		{
			void Systick_LED_4(void);
		}
		SysTick_Delay_Nms(300);
}

void LED_TASK_2(uint16_t LED_name,uint16_t LED_f)
{
	if(LED_f>5)
	{
		LED_ALL_ON();;                       
	}
	else
	{
		LED_WHICH_ON(1);
		SysTick_Delay_Nms(500);
		LED_WHICH_OFF(1);
		SysTick_Delay_Nms(500);
		LED_WHICH_ON(2);
		SysTick_Delay_Nms(250);
		LED_WHICH_OFF(2);
		SysTick_Delay_Nms(250);
		LED_WHICH_ON(3);
		SysTick_Delay_Nms(125);
		LED_WHICH_OFF(3);
		SysTick_Delay_Nms(125);
		LED_WHICH_ON(4);
		SysTick_Delay_Nms(63);
		LED_WHICH_OFF(4);
		SysTick_Delay_Nms(63);	
    }
}
#define BUZZER_T 1000
void BUZZ_TASK(uint16_t BUZZER_ON)
{
	if(BUZZER_ON>BUZZER_T)
		BUZZER_ON=1000;
	static uint16_t BUZZER_OFF;
	BUZZER_OFF=BUZZER_T-BUZZER_ON;
	BUZZ_ON;
	SysTick_Delay_Nms(BUZZER_ON);
	BUZZ_OFF;
	SysTick_Delay_Nms(BUZZER_OFF);
}
void BUZZ_TASK_2(uint16_t BUZZER_D)
{
	if(BUZZER_D>100)
		BUZZER_D=100;
	static uint16_t BUZZER_TON;
	static uint16_t BUZZER_TOFF;
	BUZZER_TON=(BUZZER_D/100)*BUZZER_T;
	BUZZER_TOFF=BUZZER_T-BUZZER_TON;
	BUZZ_ON;
	SysTick_Delay_Nms(BUZZER_TON); 
	BUZZ_OFF;
	SysTick_Delay_Nms(BUZZER_TOFF); 
	
}

#define LED_T 1000       			//LED控制周期，大概1000个延时次数
#define LED_TON 500       			//LED点亮时长，大概1000个延时次数
void LED_TASK_SCAN(uint16_t LED_F)  			//LED扫描任务，该程序不含延时， T_cnt个while中延时时长
{
	static  uint16_t T_cnt;				//局部静态变量
	if(LED_F==0)
	{
		LED_ALL_ON();
	}
	else if(LED_F<=5)
	{
		if(++T_cnt<LED_TON)
	{
		LED_BLU_ON();
	}
	else if(T_cnt<LED_T)
	{
		LED_BLU_OFF();
	}
	else
	{
		T_cnt=0;
	}
	}
	
}
#define BUZZer_T 1000
#define BUZZer_TON 500
void BUZZ_TASK_SCAN(void)
{
	static  uint16_t T_cnt;
	if(++T_cnt<BUZZer_TON)
	{
		BUZZ_ON;
	}
	else if(T_cnt<BUZZer_T)
	{
		BUZZ_OFF;
	}
	else
	{
		T_cnt=0;
	}
}
int main(void)
{
	LED_GPIO_Config();
	BUZZ_GPIO_Config();	
	while(1)
	{
		SysTick_Delay_Nms(1);
		LED_TASK_2(2,4);
		SysTick_Delay_Nms(1);
		LED_TASK_SCAN(3);
		SysTick_Delay_Nms(1);
		BUZZ_TASK(500);
		SysTick_Delay_Nms(1);
		BUZZ_TASK_SCAN();
	}
}
//按钮控制

/*uint8_t KEY_SCAN(void)
{
	static uint8_t key_old=1;
	uint8_t key_now;
	key_now=GETKEY1();
	if(key_old==1 &&key_now==0)
	{
		key_old=key_now;return 1;
	}
	else 
	{
		key_old=key_now;return 0;
	}
}*/
/*uint8_t key1_pressed=0;
void KEY_SCAN_FLAG(void)
{
	static uint8_t key_old=1;
	uint8_t key_now;
	key_now=GETKEY1();
	if(key_old==1 &&key_now==0)
		key1_pressed=1;
	key_old=key_now;
}*/
/*int main(void)
{
	uint8_t KEY_CNT=0;
	LED_GPIO_Config();初始化LED灯
	KEY_GPIO_Config();
	while(1)
	{
		
		KEY_SCAN_FLAG();
		if(key1_pressed==1)
		{
			key1_pressed=0;
			if(++KEY_CNT%2==0)LED_GEN_OFF();
			else LED_GEN_ON();
		}
	}

}*/
